﻿@using DCMS.Web.Extensions;
@using DCMS.Web.Framework;
@using DCMS.Web.Framework.UI;
@using DCMS.Core.Domain.WareHouses;
@using DCMS.ViewModel.Models.WareHouses;
@using System.Collections.Generic;
@using System.Linq;

@model StockReportListModel


<section id="content_wrapper">

    <!-- Start: Topbar-Dropdown -->
    <div id="topbar-dropmenu">
        @await Html.PartialAsync("_ToolBox")
    </div>
    <!-- End: Topbar-Dropdown -->
    <!-- Start: Topbar -->
    <header id="topbar">
        <div class="topbar-left">
            <ol class="breadcrumb">
                <li class="crumb-active">
                    <a href="#">仓库</a>
                </li>
                <li class="crumb-icon">
                    <a href="@Url.RouteUrl("HomePage")">
                        <span class="glyphicon glyphicon-home"></span>
                    </a>
                </li>
                <li class="crumb-link">
                    <a href="#">库存报表</a>
                </li>
                <li class="crumb-trail">库存表</li>
            </ol>
        </div>
        <div class="topbar-right">
            <div class="ml15 ib va-m" id="toggle_sidemenu_r">
                <a href="#" class="pl5">
                    <i class="fa fa-sign-in fs22 text-primary"></i>
                    <span class="badge badge-hero badge-danger">3</span>
                </a>
            </div>
        </div>
    </header>
    <!-- End: Topbar -->
    <!-- Begin: Content -->
    <section id="content" class="formSection">

        <div class="special-alerts">
            <div class="alert alert-danger light alert-dismissable" id="alert-demo-2" style="display: none;">
                <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
                <i class="fa fa-info pr10"></i>
                <strong>您没有被授权此操作!</strong>
            </div>
        </div>

        <div class="row">
            <div class="col-md-12">
                <form asp-action="List" asp-controller="StockReport" method="get" id="formSearch" novalidate="novalidate">

                    <div class="panel panel-default" id="panel0">
                        <div class="panel-heading">
                            <span class="glyphicon glyphicon-search"></span> 高级搜索
                            <a class="btn btn-default btn-sm pull-right m5 mrn p5 pbn" id="AdvancedSearch"><i class="fa fa-angle-double-right"></i></a>
                        </div>
                        <div class="panel-body p10">

                            <div class="row">
                                <div class="col-sm-2">
                                    <dcms-select asp-for="WareHouseId" asp-items="@Model.WareHouses" asp-default="仓库" />
                                </div>

                                <div class="col-sm-2">
                                    <dcms-select asp-for="CategoryId" asp-items="@Model.Categories" asp-default="商品类别" />
                                </div>

                                <div class="col-sm-2">
                                    <div class="input-group">
                                        <input asp-for="ProductId" type="hidden" />
                                        <input asp-for="ProductName" class="form-control" placeholder="商品..." />
                                        <span class="input-group-btn">
                                            <button id="btn_search_product" type="button" class="btn btn-default">
                                                <span class="glyphicon glyphicon-search"></span>
                                            </button>
                                        </span>
                                    </div>
                                </div>

                                <div class="col-sm-2">
                                    <dcms-select asp-for="BrandId" asp-items="@Model.Brands" asp-default="品牌" />
                                </div>

                                <div class="col-sm-2">
                                    <dcms-select-bool asp-for="Status" asp-items="@(new string[] {  "正常", "停用"})" asp-default="商品状态" />
                                </div>

                                <div class="col-sm-2">
                                    <input asp-for="MaxStock" class="form-control" placeholder="数量小于..." />
                                </div>
                            </div>

                            <div class="row mt10">
                                <div class="col-sm-2">
                                    <dcms-select-bool asp-for="ShowZeroStack" asp-items="@(new string[] {  "是", "否"})" asp-default="显示零库存" />
                                </div>
                                <div class="col-sm-10">
                                    <button id="btn_query" type="submit" class="btn btn-primary btn-block">
                                        <span class="glyphicon glyphicon-search mr5" aria-hidden="true"></span>查询
                                    </button>
                                </div>
                            </div>
                        </div>

                    </div>

                </form>

                <div class="panel">
                    <div class="panel-heading">
                        <div class="panel-title">
                            <span class="glyphicon glyphicon-tasks"></span> 库存报表
                        </div>
                    </div>
                    <div class="panel-body p10">
                        <div id="toolbar" class="btn-group mb10">
                            <a id="btn_export" class="btn btn-info btn-sm mr5">
                                <span class="glyphicon glyphicon-export mr5"></span>导出报表
                            </a>
                            <a id="btn_generate" class="btn btn-primary btn-sm mr5">
                                <span class="glyphicon glyphicon-export mr5"></span>生成期初导入模板
                            </a>
                        </div>

                        <div class="bootstrap-table">

                            <div class="fixed-table-container">
                                <div class="fixed-table-body">
                                    @{
                                        var cGroups = Model.Items.GroupBy(s => s.CategoryName);

                                        <table class="table table-striped table-bordered table-hover">
                                            <tr>
                                                <th class="text-left w100"><div class="th-inner">类别</div></th>
                                                <th class="text-center"><div class="th-inner">商品编号</div></th>
                                                <th class="text-left"><div class="th-inner">商品名称</div></th>
                                                <th class="text-center"><div class="th-inner">条形码</div></th>
                                                <th class="text-center"><div class="th-inner">单位换算</div></th>

                                                <th class="text-center"><div class="th-inner">可用库存</div></th>
                                                <th class="text-center"><div class="th-inner">现货库存</div></th>
                                                <th class="text-center"><div class="th-inner">预占库存</div></th>

                                                <th class="text-right"><div class="th-inner">成本单价(元)</div></th>
                                                <th class="text-right"><div class="th-inner">成本金额(元)</div></th>
                                                @*<th class="text-right"><div class="th-inner">批发单价(元)</div></th>
                                                <th class="text-right"><div class="th-inner">批发金额(元)</div></th>*@
                                                <th class="text-center"><div class="th-inner">出入库记录</div></th>
                                                <th class="text-center"><div class="th-inner">库存流水</div></th>
                                            </tr>

                                            @foreach (IGrouping<string, StockReportProduct> uGroups in cGroups)
                                            {
                                                var cfirst = uGroups.FirstOrDefault();
                                                <tr>
                                                    <td class="text-left" colspan="3">
                                                        <a href="#">
                                                            <i class="fa fa-caret-right mr5"></i>
                                                            <b>@cfirst?.CategoryName</b>
                                                        </a>
                                                    </td>

                                                    <td></td>
                                                    <td></td>
                                                    <td class="text-center">
                                                        @Html.Raw($"<b>{uGroups.Sum(s => s.UsableQuantityPart.Item1)} 大 {Model.Items.Sum(s => s.UsableQuantityPart.Item2)} 中 {Model.Items.Sum(s => s.UsableQuantityPart.Item3)}  小</b>")
                                                    </td>
                                                    <td class="text-center">
                                                        @Html.Raw($"<b>{uGroups.Sum(s => s.CurrentQuantityPart.Item1)} 大 {Model.Items.Sum(s => s.CurrentQuantityPart.Item2)} 中 {Model.Items.Sum(s => s.CurrentQuantityPart.Item3)} 小 </b>")
                                                    </td>

                                                    <td class="text-center">
                                                        @Html.Raw($"<b>{uGroups.Sum(s => s.OrderQuantityPart.Item1)} 大 {Model.Items.Sum(s => s.OrderQuantityPart.Item2)} 中 {Model.Items.Sum(s => s.OrderQuantityPart.Item3)} 小 </b>")
                                                    </td>

                                                    <td></td>
                                                    <td class="text-right">
                                                        @{
                                                            @Html.Raw("￥ <b>" + (uGroups.Sum(s => s.CostAmount)).ToString("0.00") + "</b>")
                                                        }
                                                    </td>
                                                    <td></td>
                                                    <td class="text-right">
                                                        @{
                                                            @Html.Raw("￥ <b>" + (uGroups.Sum(s => s.TradeAmount)).ToString("0.00") + "</b>")
                                                        }
                                                    </td>
                                                    <td class="text-center">
                                                    </td>
                                                    <td class="text-center">
                                                    </td>
                                                </tr>
                                                @foreach (var product in uGroups)
                                                {
                                                    <tr>
                                                        <td class="text-center">
                                                        </td>
                                                        <td class="text-center">
                                                            @(string.IsNullOrEmpty(product?.ProductCode) ? "-" : product?.ProductCode)
                                                        </td>
                                                        <td class="text-left">
                                                            <a href="/Product/Edit?id=@product?.ProductId" target="_blank">   @product?.ProductName</a>
                                                        </td>
                                                        <td class="text-center">

                                                            @(string.IsNullOrEmpty(product?.BigBarCode) ? "-" : product?.BigBarCode)
                                                        </td>
                                                        <td class="text-center">
                                                            @product?.UnitConversion
                                                        </td>
                                                        <td class="text-center">

                                                            @(string.IsNullOrEmpty(product?.UsableQuantityConversion) ? "-" : product?.UsableQuantityConversion)
                                                        </td>
                                                        <td class="text-center">

                                                            @(string.IsNullOrEmpty(product?.CurrentQuantityConversion) ? "-" : product?.CurrentQuantityConversion)
                                                        </td>

                                                        <td class="text-center">

                                                            @(string.IsNullOrEmpty(product?.OrderQuantityConversion) ? "-" : product?.OrderQuantityConversion)
                                                        </td>

                                                        <td class="text-right">
                                                            @product?.CostPrice.ToString("0.00")
                                                        </td>
                                                        <td class="text-right">
                                                            @product?.CostAmount.ToString("0.00")
                                                        </td>
                                                        @*<td class="text-right">
                                                            @product?.TradePrice.ToString("0.00")
                                                        </td>
                                                        <td class="text-right">
                                                            @product?.TradeAmount.ToString("0.00")
                                                        </td>*@
                                                        <td class="text-center">
                                                            <a href='javascript:void(0)' class="showStockInOutRecords" data-id="@product.ProductId">查看</a>
                                                        </td>
                                                        <td class="text-center">
                                                            <a href='javascript:void(0)' class="showStockFlows" data-id="@product.ProductId">查看</a>
                                                        </td>
                                                    </tr>
                                                }
                                            }

                                            <tr>
                                                <td class="text-right" colspan="5">合计:</td>

                                                <td class="text-center">
                                                    @Html.Raw($"<b>{Model.Items.Sum(s => s.UsableQuantityPart.Item1)} 大 {Model.Items.Sum(s => s.UsableQuantityPart.Item2)} 中 {Model.Items.Sum(s => s.UsableQuantityPart.Item3)}  小</b>")
                                                </td>
                                                <td class="text-center">
                                                    @Html.Raw($"<b>{Model.Items.Sum(s => s.CurrentQuantityPart.Item1)} 大 {Model.Items.Sum(s => s.CurrentQuantityPart.Item2)} 中 {Model.Items.Sum(s => s.CurrentQuantityPart.Item3)} 小 </b>")
                                                </td>
                                                <td class="text-center">
                                                    @Html.Raw($"<b>{Model.Items.Sum(s => s.OrderQuantityPart.Item1)} 大 {Model.Items.Sum(s => s.OrderQuantityPart.Item2)} 中 {Model.Items.Sum(s => s.OrderQuantityPart.Item3)} 小 </b>")
                                                </td>

                                                <td></td>
                                                <td class="text-right">
                                                    @{
                                                        @Html.Raw("￥ <b>" + (Model.Items.Sum(s => s.CostAmount)).ToString("0.00") + "</b>")
                                                    }
                                                </td>
                                                <td></td>
                                                <td class="text-right">
                                                    @{
                                                        @Html.Raw("￥ <b>" + (Model.Items.Sum(s => s.TradeAmount)).ToString("0.00") + "</b>")
                                                    }
                                                </td>
                                                <td class="text-center">
                                                </td>
                                                <td class="text-center">
                                                </td>
                                            </tr>
                                        </table>
                                    }

                                </div>
                            </div>

                            @{
                                var pager = Html.Pager(Model.PagingFilteringContext).QueryParam("pagenumber");
                            }
                            @if (!string.IsNullOrEmpty(pager.ToHtmlString()))
                            {
                                <div class="fixed-table-pagination" style="display: block;">
                                    @Html.Raw(pager.ToHtmlString())
                                </div>
                            }
                            <div class="clearfix"></div>
                        </div>

                    </div>
                </div>
            </div>
        </div>

        <!-- End: Content -->
    </section>

</section>



@await Html.PartialAsync("_Windows", Model)

@section CurPageScripts
{
    <script type="text/javascript">
        jQuery(document).ready(function () {

            "use strict";

            // Init Theme Core
            Core.init();

            // Init Demo JS
            Demo.init();

            //权限提示
            if ($(this).getUrlQueryString("Authorize") == "noAuthorize") {
                $('#alert-demo-2').fadeToggle();
                setTimeout(function () {
                    $('#alert-demo-2').fadeToggle();
                }, 3000);
            }

            //高级搜索
            $("#panel0 #AdvancedSearch").click(function () {
                var ico = $(this).find(".fa");
                if (ico.hasClass('fa-angle-double-right'))
                    ico.removeClass("fa-angle-double-right").addClass("fa-angle-double-down");
                else
                    ico.removeClass("fa-angle-double-down").addClass("fa-angle-double-right");
                $("#panel0").find(".panel-body").toggleClass("hidden fadeIn");
            });

            //商品列表选择赋值
            window.operateEventsPop = {
                'click .rowSelect': function (e, value, row, index) {
                    var index = $(this).attr("data-id");
                    var keys = Object.keys(row.Units);
                    $("#ProductName").val(row.Name);
                    $("#ProductId").val(row.Id);
                    $('#ModalProductWindows').modal('hide');
                }
            };

            $("#btn_search_product").click(function () {
                $(this).productSelectShowModal($("#ModalProductWindows"), "/Product/AsyncSearchSelectPopup", null, "选择商品", $("#ProductModalForm"),
                    function () { });
            });

            //防止重复查询
            var queryNumber = 1;
            $("#btn_query").click(function () {
                if (queryNumber > 1) {
                    return false;
                }
                queryNumber = queryNumber + 1;
            });

            //用户修改商品名称，将商品Id置零
            $("#ProductName").bind("change", function () {
                $("#ProductId").val(0);
            });

            //导出
            $("#btn_export").click(function () {
                window.location.href = "/StockReport/ExportList?wareHouseId=" + $("#WareHouseId").val() + "&categoryId=" + $("#CategoryId").val() + "&productId=" + $("#ProductId").val() + "&productName=" + $("#ProductName").val() + "&brandId=" + $("#BrandId").val() + "&status=" + $("#Status").val() + "&maxStock=" + $("#MaxStock").val() + "&showZeroStack=" + $("#ShowZeroStack").val() + "&pagenumber=@Context.Request.Query["pagenumber"]";
            });

            //生成期初导入模板
            $("#btn_generate").click(function () {
                window.location.href = "/StockReport/ExportGenerateList?wareHouseId=" + $("#WareHouseId").val() + "&categoryId=" + $("#CategoryId").val() + "&productId=" + $("#ProductId").val() + "&productName=" + $("#ProductName").val() + "&brandId=" + $("#BrandId").val() + "&status=" + $("#Status").val() + "&maxStock=" + $("#MaxStock").val() + "&showZeroStack=" + $("#ShowZeroStack").val();
            });


            //获取商品出入库记录
            $(document).on('click', ".showStockInOutRecords", function () {
                var productId = $(this).attr("data-id");
                $(this).showModalV2($("#ModalRecordsWindows"), "/StockReport/AsyncPopupWindow", { categoryId: 0, productId: productId }, "出入库记录", $("#RecordsModalForm"), function (rows) { });
            });


            //获取商品出入库流水
            $(".showStockFlows").click(function () {
                var productId = $(this).attr("data-id");
                $(this).showModalV2($("#ModalRecordsWindows"), "/StockReport/AsyncPopupWindow", { categoryId: 1, productId: productId }, "出入库流水", $("#RecordsModalForm"), function (rows) { });
            });
        });
    </script>
}
